<template>
  <div class="randoms-com">
    <el-card class="box-card">
      <div class="search">
        <div class="search-left">
          <div class="one">
            <span>关键字</span>
            <el-input
              v-model="FormData.keyword"
              placeholder="根据文章标题搜索"
            ></el-input>
          </div>
          <div class="one">
            <span style="margin-right: -27px">状态</span>
            <el-select v-model="FormData.state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
          <div class="right">
            <el-button @click="clearinpt">清空</el-button>
            <el-button type="primary" @click="onsearch">搜索</el-button>
          </div>
        </div>
        <div class="add">
          <el-button type="success" icon="el-icon-edit" @click="addSkill('add')">新增技巧</el-button>
        </div>
      </div>
      <el-alert
        :title="`数据一共${counts}条`"
        type="info"
        :closable="false"
        show-icon
      >
      </el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="title" label="文章标题" width="230">
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="130">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="130">
        </el-table-column>

        <el-table-column prop="createTime" label="录入时间" width="230">
        </el-table-column>
        <el-table-column label="启用状态">
          <template #default="{ row }">
            <!-- 0禁用  1启用 -->
            <span>{{ row.state === 0 ? "已禁用" : "已启用" }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <!-- <template slot-scope="scope"> -->
          <template #default="{ row }">
            <el-button type="text" @click="viewdetail(row.id)">预览</el-button>
            <el-button type="text" @click="stateArticles(row)">{{ row.state === 0 ? "开启" : "禁用" }}</el-button>
            <el-button type="text" :disabled="row.state === 1" @click="addSkill('edit',row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state === 1" @click="delSkill(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="FormData.page"
        :page-sizes="[2, 3, 5, 6]"
        :page-size="FormData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </el-card>
    <articles-dialog @addSuccess="addSuccess" v-if="dialogVisible" v-model="dialogVisible" :id="editId"></articles-dialog>
    <articles-view :id="id" v-if="viewDialog" v-model="viewDialog"></articles-view>
  </div>
</template>

<script>
import { getArticlesListRequest, stateArticlesRequest, delSkillRequest } from '../../api/hmmm/articles-list'
import articlesDialog from '../components/articles-dialog.vue'
import ArticlesView from '../components/articles-view.vue'
export default {
  components: { articlesDialog, ArticlesView },

  data () {
    return {
      input: '',
      counts: 0,
      tableData: [],
      FormData: {
        page: 1,
        pagesize: 5,
        keyword: '',
        state: null
      },
      options: [
        { value: 0, label: '已禁用' },
        { value: 1, label: '已启用' }
      ],
      dialogVisible: false,
      id: 0,
      viewDialog: false,
      editId: 0,
      state: ''
    }
  },
  created () {
    this.getArticlesList()
  },
  methods: {
    async delSkill (id) {
      await delSkillRequest(id)
      this.$message.success('删除成功')
      this.getArticlesList()
    },
    async stateArticles (row) {
      if (row.state === 1) {
        this.state = 0
      } else if (row.state === 0) {
        this.state = 1
      }
      await stateArticlesRequest({ id: row.id, state: this.state })
      this.$message.success('操作成功')
      this.getArticlesList()
    },
    viewdetail (id) {
      this.viewDialog = true
      this.id = id
    },
    // 添加成功
    addSuccess () {
      this.getArticlesList()
    },
    // 新增技巧
    addSkill (val, id) {
      this.dialogVisible = true
      if (val === 'edit') {
        this.editId = id
      }
    },
    clearinpt () {
      this.FormData.keyword = ''
      this.FormData.state = ''
    },
    onsearch () {
      this.getArticlesList()
    },
    async getArticlesList () {
      const res = await getArticlesListRequest(this.FormData)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    handleSizeChange (val) {
      this.FormData.pagesize = val
      this.getArticlesList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.FormData.page = val
      this.getArticlesList()
      // console.log(`当前页: ${val}`)
    }
  }
}
</script>
<style>
</style>
<style scoped lang='scss'>
.randoms-com {
  padding: 20px;
  .box-card {
    padding: 20px;
    .search {
      display: flex;
      justify-content: space-between;
      .search-left {
        display: flex;
        .one {
          display: flex;
          align-items: center;
          margin-right: 20px;
          span {
            width: 80px;
            font-size: 14px;
            color: #606266;
            font-weight: 700;
          }
        }
      }
    }
    ::v-deep .el-alert {
      margin: 15px 0;
    }
    .el-pagination {
      margin-top: 15px;
      text-align: right;
    }
  }
}
</style>
